package com.ruoyi.pos.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.pos.domain.Order;
import com.ruoyi.pos.service.IOrderItemService;
import com.ruoyi.pos.service.IOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pos.domain.Payment;
import com.ruoyi.pos.service.IPaymentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * paymentController
 * 
 * @author ruoyi
 * @date 2021-05-03
 */
@RestController
@RequestMapping("/dev-api/system/pos/payment")
public class PaymentController extends BaseController
{
    @Autowired
    private IPaymentService paymentService;
    @Autowired
    private IOrderService orderService;

    @GetMapping(value = "/code/{code}")
    public AjaxResult getInfo(@PathVariable("code") String orderCode)
    {
        return AjaxResult.success(paymentService.selectPaymentByOrderCode(orderCode));
    }
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody Payment payment){
        String orderCode=payment.getOrderCode();
        Order order=orderService.selectOrderByCode(orderCode);
//        将order和payment的共同数据填充
        payment.setAmount(order.getTotal());
        payment.setOpId(order.getOpId());
        payment.setShopId(order.getShopId());
//        计算实收金额 实收=应收*（1-discount)
        payment.setActualMoney(payment.getAmount().multiply(new BigDecimal(1-payment.getDiscount())));
        payment.setPayDate(new Date());
        payment.setPaymentCode(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        paymentService.insertPayment(payment);
        return AjaxResult.success(payment.getId());
    }

    /**
     * 查询payment列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Payment payment)
    {
        startPage();
        List<Payment> list = paymentService.selectPaymentList(payment);
        return getDataTable(list);
    }

    /**
     * 导出payment列表
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:export')")
    @Log(title = "payment", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Payment payment)
    {
        List<Payment> list = paymentService.selectPaymentList(payment);
        ExcelUtil<Payment> util = new ExcelUtil<Payment>(Payment.class);
        return util.exportExcel(list, "payment");
    }

    /**
     * 获取payment详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(paymentService.selectPaymentById(id));
    }

    /**
     * 新增payment
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:add')")
    @Log(title = "payment", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payment payment)
    {
        return toAjax(paymentService.insertPayment(payment));
    }

    /**
     * 修改payment
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:edit')")
    @Log(title = "payment", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payment payment)
    {
        return toAjax(paymentService.updatePayment(payment));
    }

    /**
     * 删除payment
     */
//    @PreAuthorize("@ss.hasPermi('pos:payment:remove')")
    @Log(title = "payment", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(paymentService.deletePaymentByIds(ids));
    }
}
